package com.lyb.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lyb.mapper.ArticleMapper;
import com.lyb.pojo.Article;
import com.lyb.service.ArticleService;
import com.lyb.utils.Result;
import com.lyb.utils.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleMapper mapper;
    @Override
    public Result findPage(int page, int size) {
        PageHelper.startPage(page, size);
        List<Article> articles = mapper.findPage();
        PageInfo<Article> pageInfo = new PageInfo(articles);
        return Result.build(pageInfo.getList(), ResultCodeEnum.SUCCESS);
    }

    @Override
    public Result queryList(Integer cid) {
        List<Article> articleList = mapper.queryList(cid);
        return Result.build(articleList,ResultCodeEnum.SUCCESS);
    }

    @Override
    public Result queryDetail(Integer id) {
        Article article = mapper.queryById(id);
        return Result.build(article,ResultCodeEnum.SUCCESS);
    }

    @Override
    public Result save(Article article) {
        article.setCreateTime(new Date());
        article.setUpdateTime(new Date());
        mapper.save(article);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    @Override
    public Result update(Article article) {
        article.setUpdateTime(new Date());
        mapper.update(article);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    @Override
    public Result delete(Integer id) {
        mapper.delete(id);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }
}
